var app = getApp();
// pages/order/downline.js
Page({
  data: {
    itemData: {},
    userId: 0,
    paytype: 'weixin',//0线下1微信
    remark: '',
    addrId: 0,//收货地址//测试--
    btnDisabled: false,
    productData: [],
    address: {},
    goodsurl: app.d.hostUrl + app.d.goodsurl,
    vipgood: [],
  },
  onLoad: function (options) {
    console.log(options);
    var that = this;
    var free = parseInt(options.free);
    this.freeinit();
    var vip = parseInt(options.vip);
    if (free == 0){
      var postage = options.postage;
    }else{
      var postage = 0;
    }
    
    if ((free < 0 || free > 1) || (vip < 1 || vip > 2)){
      wx.showToast({
        title: "非法请求",
        duration: 2000
      });
      wx.switchTab({
        url: '../index/index'
      });
      return;
    }
    
    if (vip == 1) {
      wx.getStorage({
        key: 'king',
        success(res) {
          that.setData({ vipgood: res.data, vip: vip, postage: postage })
        }
      });
    }
    if (vip == 2 ){
      wx.getStorage({
        key: 'zun',
        success(res) {
          that.setData({ vipgood: res.data, vip: vip, postage: postage })
        }
      });
    }

  },
  freeinit: function () {
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_addr.html',
      method: 'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data.data)
        var adds = res.data.data.consignee_list;
        if (adds) {
          var addrId = adds.id;
          that.setData({
            address: adds,
            addrId: addrId,
            addemt: res.data.data.addemt,
            free:res.data.data.free
          });
        } else {
          wx.showToast({
            title: "请添加收货地址",
            duration: 3000
          });
        }
        console.log(res.data.data.free)
      },
    });
  },
  freeget:function(){
    var that = this; console.log(that.data.vipgood);
    wx.request({
      url: app.d.hostUrl + 'wapi/save_free.html',
      method: 'post',
      data: {
        goods_id: that.data.vipgood.goods_id,
        vip: that.data.vip,
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        addemt: that.data.addrId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        
        var data = res.data; console.log(data.ret);
        if (data.ret == 0) {
          app.globalData.receive = 1;
          wx.showToast({
            title: "领取成功",
            duration: 3000
          });
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/index/index',
            });
          }, 2000)
        }
        if (data.ret == 1) {
          wx.showToast({
            title: data.msg,
            duration: 3000
          });
          that.wxpay(data.data);
        }
        
        //endInitData
      },
    });
  },
  //调起微信支付
  wxpay: function (order) {
    console.log(order)
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/wxpay_postage.html',
      data: {
        order_id: order.id,
        openid: app.globalData.userInfo.openid,
        seesionid: app.globalData.userInfo.sessionId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }, // 设置请求的 header
      success: function (res) {
        if (res.data.status == 0) {
          var order = res.data.data;
          wx.requestPayment({
            timeStamp: order.timeStamp,
            nonceStr: order.nonceStr,
            package: order.package,
            signType: 'MD5',
            paySign: order.paySign,
            success: function (res) {
              wx.showToast({
                title: "支付成功!",
                duration: 2000,
              });
              app.globalData.receive = 1; console.log(444);
              wx.switchTab({
                url: '/pages/index/index',
              }); console.log(555);
            },
            fail: function (res) {
              wx.showToast({
                title: res,
                duration: 3000
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！err:wxpay',
          duration: 2000
        });
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: '极好生活商城',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
});